package com.wrench.elevator.tida.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.wrench.elevator.tida.domain.wuye.WebUIFormatData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.dao.WYZHDao;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.domain.wuye.Wuye_User;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.node.NodeProvince;
import com.wrench.elevator.tida.util.WrenchUtil;

@Component
public class WYZHService
{
    private static final Logger logger = LoggerFactory.getLogger(JSGLService.class);
    
    @Resource
    private WYZHDao dao;


    public List<WebUIFormatData> getWholeRegion(String wuye_id) throws WrenchException {
        if(WrenchUtil.hasNull(wuye_id)){
            logger.error("==> param is not found , wuye_id = {} ",wuye_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE,Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        List<WebUIFormatData> data=null;
        try {
            data=dao.getWholeRegion(wuye_id);
        } catch (Exception e) {
           logger.error("==> data=dao.getWholeRegion(wuye_id) execute error , wuye_id = {} , error = {} ",wuye_id,e);
           throw new WrenchException(Result.DB_ERROR_CODE,Result.DB_ERROR_MESSAGE);
        }

        return data;

    }

    
    public Map<String,String> getSSQID(String id,String type) throws WrenchException{
    	if(WrenchUtil.hasNull(id,type)){
    		logger.error("==> param is not found , id = {} , type = {}  ",id,type);
    		throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
    	}
    	Map<String,String> ssqId=null;
    	try {
			ssqId=dao.getSSQID(id,type);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("==> ssqId=dao.getSSQID(id,type) execute error , id = {} , type = {} , error = {}  ",id,type,e);
			throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
		}
    	return ssqId;
    }
    
    public List<NodeProvince> getAllNodesBuWuye_id(String user_id)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(user_id))
        {
            logger.error("==> param is not found , user_id = {}", user_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchUtil.areInt(user_id))
        {
            logger.error("==> string is not int type , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        List<NodeProvince> nodeProvinces = null;
        try
        {
            nodeProvinces = dao.getAllNodesBuWuye_id(user_id);
        }
        catch (Exception e)
        {
            logger.error("==> nodeProvinces=dao.getAllNodesBuWuye_id(user_id) execute error , user_id  = {} , error = {} ",
                user_id,
                e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        return nodeProvinces;
        
    }
    
    public void bindWuYeUserManage(String user_id, String[] province_id, String[] city_id, String[] district_id,
        String[] building_id, ModelMap map)
        throws WrenchException
    {
        if(null==province_id&&null==city_id&&null==district_id&&null==building_id){
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }

        if (WrenchUtil.hasNull(user_id))
        {
            logger.error("==> param is not found , user_id = {}", user_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchUtil.areInt(user_id))
        {
            logger.error("==> string is not int type , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        String provinces_id = null;
        String citys_id = null;
        String districts_id = null;
        String buildings_id = null;
        if (!WrenchUtil.hasNull(user_id, province_id))
        {
            StringBuffer sb = new StringBuffer();
            int len = province_id.length;
            for (int i = 0; i < len; i++)
            {
                sb.append(province_id[i]);
                if (i != len - 1)
                    sb.append(",");
            }
            provinces_id = sb.toString();
        }
        if (!WrenchUtil.hasNull(user_id, city_id))
        {
            StringBuffer sb = new StringBuffer();
            int len = city_id.length;
            for (int i = 0; i < len; i++)
            {
                sb.append(city_id[i]);
                if (i != len - 1)
                    sb.append(",");
            }
            citys_id = sb.toString();
        }
        if (!WrenchUtil.hasNull(user_id, district_id))
        {
            StringBuffer sb = new StringBuffer();
            int len = district_id.length;
            for (int i = 0; i < len; i++)
            {
                sb.append(district_id[i]);
                if (i != len - 1)
                    sb.append(",");
            }
            districts_id = sb.toString();
        }
        if (!WrenchUtil.hasNull(user_id, building_id))
        {
            StringBuffer sb = new StringBuffer();
            int len = building_id.length;
            for (int i = 0; i < len; i++)
            {
                sb.append(building_id[i]);
                if (i != len - 1)
                    sb.append(",");
            }
            buildings_id = sb.toString();
        }
        
        logger.debug("==> user_id = {} , provinces_id = {} , citys_id = {} , districts_id = {}, buildings_id = {} ",
            user_id,
            provinces_id,
            citys_id,
            districts_id,
            buildings_id);
        
        WrenchUtil.getParamMap(map,
            "user_id",
            user_id,
            "provinces_id",
            provinces_id,
            "citys_id",
            citys_id,
            "districts_id",
            districts_id,
            "buildings_id",
            buildings_id);
        try
        {
            dao.bindWuYeUserManage(map);
        }
        catch (Exception e)
        {
            logger.error("==> dao.bindWuYeUserManage(map) execute error , map = {} , error = {}",
                JSON.toJSONString(map),
                e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        
        logger.error("==> dao.bindWuYeUserManage(map) execute success , map = {} ", JSON.toJSONString(map));
    }
    
    public Page queryWuYeUser(String name, String current_page, String limit)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(name, current_page, limit))
        {
            logger.error("==> param is not found , name = {}  , current_page = {} , limit = {}",
                name,
                current_page,
                limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchUtil.areInt(current_page, limit))
        {
            logger.error("==> string is not int type , current_page = {} , limit = {} ", current_page, limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        List<Wuye_User> wuyeUserList = null;
        
        try
        {
            wuyeUserList =
                dao.queryWuyeUser(name,
                    (Integer.valueOf(current_page) - 1) * Integer.valueOf(limit),
                    Integer.valueOf(limit));
        }
        catch (Exception e)
        {
            logger.error("==>  wuyeUserList=dao.queryWuyeUser(name, Integer.valueOf(current_page)* Integer.valueOf(limit),  Integer.valueOf(limit)) execute error ,   name = {}  , current_page = {} , limit = {} , error = {} ",
                name,
                current_page,
                limit,
                e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        
        if (WrenchUtil.hasNull(wuyeUserList))
        {
            logger.error("==>  wuyeUserList=dao.queryWuyeUser(name, Integer.valueOf(current_page)* Integer.valueOf(limit),  Integer.valueOf(limit)) execute success but wuyeUserList is null ,   name = {}  , current_page = {} , limit = {} , wuyeUserList = {} ",
                name,
                current_page,
                limit,
                wuyeUserList);
            throw new WrenchException(Result.QUERY_WUYE_USER_FAIL_CODE, Result.QUERY_WUYE_USER_FAIL_MESSAGE);
        }
        logger.debug("==>  wuyeUserList=dao.queryWuyeUser(name, Integer.valueOf(current_page)* Integer.valueOf(limit),  Integer.valueOf(limit)) execute success  ,   name = {}  , current_page = {} , limit = {} , wuyeUserList = {} ",
            name,
            current_page,
            limit,
            wuyeUserList);
        int total = -1;
        try
        {
            total = dao.queryWuyeUserCount(name);
        }
        catch (Exception e)
        {
            logger.error("==> total=dao.queryWuyeUserCount(name) execute error , name = {} , error = {} ", name, e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        
        if (total < 0)
        {
            logger.error("==>  total=dao.queryWuyeUserCount(name) execute  success but total <0  ,name = {} , total = {} ",
                name,
                total);
            throw new WrenchException(Result.QUERY_WUYE_USER_COUNT_FAIL_CODE, Result.QUERY_WUYE_USER_COUNT_FAIL_MESSAGE);
        }
        logger.debug("==>  total=dao.queryWuyeUserCount(name) execute  success  ,name = {} , total = {} ", name, total);
        
        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setTotal(total);
        page.setList(wuyeUserList);
        return page;
    }
    
    public void delWuYeUser(String user_id, ModelMap map)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(user_id))
        {
            logger.error("==> param is not found , user_id = {} ", user_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
            
        }
        if (!WrenchUtil.isInt(user_id))
        {
            logger.error("==> string is not int type , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        WrenchUtil.getParamMap(map, "user_id", user_id);
        try
        {
            dao.delWuYeUser(map);
        }
        catch (Exception e)
        {
            logger.error("==> dao.delWuYeUser(map) execute error , map = {} , error = {} ", JSON.toJSONString(map), e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.delWuYeUser(map) execute SUCCESS , map = {}  ", JSON.toJSONString(map));
    }
    
    public void addWuYeUser(String company_name, String user_name, String user_tel, String superior_id, String role_id,
        ModelMap map)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(company_name, user_name, user_tel, superior_id, role_id))
        {
            logger.error("==> param is not found , company_name = {} , user_name = {} , user_tel = {} , superior_id = {} , role_id = {} ",
                company_name,
                user_name,
                user_tel,
                superior_id,
                role_id);
            
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchUtil.areInt(superior_id, role_id))
        {
            logger.error("==> string is not int type , superior_id = {} , role_id = {} ", superior_id, role_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        WrenchUtil.getParamMap(map,
            "company_name",
            company_name,
            "user_name",
            user_name,
            "user_tel",
            user_tel,
            "superior_id",
            superior_id,
            "role_id",
            role_id);
        
        try
        {
            dao.addWuYeUser(map);
        }
        catch (Exception e)
        {
            logger.error("==> dao.addWuYeUser(map) execute error , map = {} , error = {} ", JSON.toJSONString(map), e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.addWuYeUser(map) execute SUCCESS , map = {}  ", JSON.toJSONString(map));
    }
    
    public void updateWuYeUser(String user_id, String company_name, String user_name, String user_tel,
        String superior_id, String role_id, ModelMap map)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(user_id, company_name, user_name, user_tel, superior_id, role_id))
        {
            logger.error("==> param is not found , user_id = {} ,company_name = {} , user_name = {} , user_tel = {} , superior_id = {} , role_id = {} ",
                user_id,
                company_name,
                user_name,
                user_tel,
                superior_id,
                role_id);
            
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchUtil.areInt(user_id, superior_id, role_id))
        {
            logger.error("==> string is not int type , user_id = {} , superior_id = {} , role_id = {} ",
                user_id,
                superior_id,
                role_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        WrenchUtil.getParamMap(map,
            "company_name",
            company_name,
            "user_name",
            user_name,
            "user_tel",
            user_tel,
            "superior_id",
            superior_id,
            "role_id",
            role_id,
            "user_id",
            user_id);
        
        try
        {
            dao.updateWuYeUser(map);
        }
        catch (Exception e)
        {
            logger.error("==> dao.updateWuYeUser(map) execute error , map = {} , error = {} ",
                JSON.toJSONString(map),
                e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.updateWuYeUser(map) execute SUCCESS , map = {}  ", JSON.toJSONString(map));
    }
    
}
